package cn.zhaoyuening.crm.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 登录
 * @author Zhao
 *
 */
import com.opensymphony.xwork2.ModelDriven;

import cn.zhaoyuening.crm.dao.StaffDao;
import cn.zhaoyuening.crm.domain.CrmDepartment;
import cn.zhaoyuening.crm.domain.CrmStaff;
import cn.zhaoyuening.crm.service.DepartmentService;
import cn.zhaoyuening.crm.service.StaffService;
public class StaffAction extends BaseAction<CrmStaff> {
	private CrmStaff staff = new CrmStaff();
	private CrmStaff loginStaff = new CrmStaff(); 
	private StaffService staffService;
	private DepartmentService departmentService;
		
	public String login(){
		//连接进入 直接进入登录界面
		/*if (this.staff.getLoginName()==null&&this.staff.getLoginPwd()==null) {
			return LOGIN;
		}*/
		CrmStaff loginStaff = staffService.login(this.loginStaff);
		if (staff!=null) {
			//登录成功
			//保存到session
			ActionContext.getContext().getSession().put("loginStaff", loginStaff);
			return SUCCESS;
		}
		//登录失败
		this.addFieldError("", "用户名与密码不匹配!");
		//返回登录界面
		return LOGIN;
	}
	
	//列出所有员工
	public String list(){
		//获取所有的staff
		List<CrmStaff> staffList = staffService.findAll();
		ActionContext.getContext().put("staffList", staffList);
		return "list";
	}

	//进入首页
	public String home(){
		return "home";
	}
	
	//编辑员工页面回显
	public String editUi(){
		this.staff = staffService.getCrmStaffById(this.staff.getStaffId());
		
		addAllDepartments2ValueContext();
		
//		ActionContext.getContext().getValueStack().push(staff);
		
		return "editUi";
	}
	
	//save edit data
	public String saveEdit(){
		staffService.updateStaff(this.staff);
		//添加提示
		addActionMessage("编辑员工"+this.staff.getStaffName()+"成功");
		//返回员工
		//this.staff = new CrmStaff();
		return list();
	}
	
	//add staff ui
	public String addStaffUi(){
		addAllDepartments2ValueContext();
		return "addStaffUi";
	}
	
	//add staff
	public String addStaff(){
		staffService.addStaff(this.staff);
		addActionMessage("添加员工："+this.staff.getStaffName()+" 成功!");
		return list();
	}
	
	//delete staff
	public String deleteStaff(){
		staffService.deleteStaff(this.staff);
		addActionMessage("删除员工："+this.staff.getStaffName()+" 成功!");
		return list();
	}
	//========private method===========
	/**
	 * 添加department到session
	 */
	public void addAllDepartments2ValueContext(){
		List<CrmDepartment> allDepartments = departmentService.findAll();
		ActionContext.getContext().put("allDepartments", allDepartments);
	}
	
	//======get&set======
	public CrmStaff getStaff() {
		return staff;
	}

	public void setStaff(CrmStaff staff) {
		this.staff = staff;
	}
	//按名称注入
	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public CrmStaff getLoginStaff() {
		return loginStaff;
	}
	
	public void setLoginStaff(CrmStaff loginStaff) {
		this.loginStaff = loginStaff;
	}

	
}
